<?php
namespace Kuoqi\Input;

//过滤方法合集
class Filter{
	
	//对数组进行规整处理
	static public function FilterArray($client){
		if(!is_array($client->value)){
			$client->value = $client->paramdef;
		}
	}
	
	//文本域转义
	static public function FilterConts($client){
		if($client->value)
		{
			$client->value = htmlspecialchars($client->value);
		}else{
			$client->value = $client->paramdef;
		}
		return $client->value;	
	}
	
	//对时间字符串的简单验证
	static public function FilterDatetime($client){
		if($client->value)
		{
			$client->value = htmlentities(trim($client->value));
		}else{
			$client->value = $client->paramdef;
		}
		return $client->value;	
	}
	
	//对字串验证是否为email
	static public function FilterEmail($client){
	    //dingdh1@sunac.com.cn
        //kuoqps@163.com
		$status = preg_match('/^([a-zA-Z]|[0-9])(\w|\-)+@([a-zA-Z0-9]+\.)+(com|cn|net)$/',$client->value);
		if(!$status){
			$client->value = $client->paramdef;	
		}
		return $client->value;
	}

    /**
     * 忽略模块的相关信息
     * @param $client
     * @return mixed
     */
	static public function FilterIgnore($client){
        if($client->value==null){
            $client->value = $client->paramdef;
        }
        return $client->value;
    }
	//对数字进行整型的处理
	static public function FilterInt($client){
		if(strlen($client->value)==0){
			$client->value = $client->paramdef;
		}else{
			$client->value = intval($client->value);	
		}
		return $client->value;
	}

    /**
     * 对json数据进行验证
     * @param $client
     * @return string
     */
    static public function FilterJson($client){
	    if($client->value==null){
            $client->value = $client->paramdef;
        }
        if(($otp = json_decode($client->value))==null){
            $otp = [];
        }
        $client->value = json_encode($otp,true);
        return $client->value;
    }
	
	static public function FilterMd5($client){
		$status = preg_match('/^[0-9a-zA-Z]{32}$/',$client->value);
		if(!$status){
			$client->value = $client->paramdef;	
		}	
		return $client->value;
	}
	
	//验证是否大陆手机号 
	static public function FilterPhone($client){
		$status = preg_match('/^1[0-9]{10}$/',$client->value);
		if(!$status){
			$client->value = $client->paramdef;	
		}	
		return $client->value;
	}
	
	//对于字符串的简单过滤
	//只过滤了空格跟双引号
	static public function FilterString($client){
		if(strlen($client->value)==0){
			$client->value = (string)$client->paramdef;
		}else{
			$client->value = trim($client->value);
			$client->value = str_replace('"','&quot;',$client->value);
			//$client->value = str_replace(' ','&nbsp;',$client->value);
			$client->value = str_replace('<','&lt;',$client->value);
			$client->value = str_replace('>','&gt;',$client->value);
		}
		return $client->value;
	}
	
	
	
	//核验预留用户名
	static public function FilterUsername($client){
		$client->value = htmlentities($client->value);
		$status = preg_match('/^[0-9a-zA-Z\x{4e00}-\x{9fa5}]{5,20}$/u',$client->value);
		if(!$status){
			$client->value = $client->paramdef;	
		}
		return $client->value;
	}

	static public function FilterUrldecode($client){
        if($client->value==null){
            $client->value = $client->paramdef;
        }
        return urldecode($client->value);
    }

	static public function FilterUrlencode($client){
        if($client->value==null){
            $client->value = $client->paramdef;
        }
        return urlencode($client->value);
    }
	
}